<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1>添加新闻</h1>
    <form action="/admin/adddata" method="post" onsubmit="return false">
        新闻标题： <input type="text" name="title" id="title"><br>
        新闻内容 ： <input type="text" name="content" id="content"><br>
        <input type="submit" value="提交" class="sub">
    </form>
    <a href="/index/shownews">点击阅读新闻</a>
</body>
<script>
    let subEle = document.querySelector(".sub");
    let titleEle = document.querySelector("#title");
    let contentEle = document.querySelector("#content");
    subEle.onclick = function () {
        let title = titleEle.value;
        let content = contentEle.value;
        console.log(title, content);
        let xhr = new XMLHttpRequest();
        xhr.open("post", "/admin/adddata");
        xhr.setRequestHeader("Content-Type", "application/json");
        xhr.send(JSON.stringify({ title, content }));
    }
</script>

</html>